
from zoom import zoomer

import dzentity, dzpage, dzutil
import models.subject

#------------------------------------------------------------------------
class SubjectListView(dzentity.Entity):
    def __init__(self):
        self.data = zoomer.db('select * from dz_subjects name')

    def get_LIST(self):
        labels = ['Name','Description']
        items = [(i.subjectid,i.name,i.descr) for i in self.data]
        return dzutil.browse(items,labels=labels,con='subject',on_click='show')

    def get_COUNT(self):
        return len(self.data)

    def render(self):
        page = dzpage.Page(dzutil.render('views/subjects.txt',self))
        page.actions = [dzutil.link_to('Add Subject',action='add',con='subject')]
        return page.render()

class SubjectShowView(dzentity.Entity):
    def __init__(self,id):
        data = zoomer.db('select * from dz_subjects where subjectid=%s',id)
        dzentity.Entity.__init__(self,data[0])

    def render(self):
        page = dzpage.Page(dzutil.render('views/subject_show.txt',self))
        page.actions = [
            dzutil.link_to('Edit',action='edit',key=self.SUBJECTID),
            dzutil.link_to('Delete',action='confirm_delete',key=self.SUBJECTID),
        ]
        return page.render()

class SubjectDeleteView(dzentity.Entity):
    def __init__(self,id):
        user = models.subject.Subjects().seek(id)
#        data = zoomer.db('select * from dz_subjects where subjectid=%s',id)
        dzentity.Entity.__init__(self,user)

    def render(self):
        page = dzpage.Page(dzutil.render('views/subject_delete.txt',form=1,con='subject',action='delete',data=self))
        return page.render()

class SubjectEditView(SubjectShowView):
    def render(self):
		page = dzpage.Page(dzutil.render('views/subject_edit.txt',form=1,con='subject',action='update',data=self))
		return page.render()

class SubjectAddView:
    def render(self):
        from models.subject import Subject
        page = dzpage.Page()
        page.content = dzutil.render('views/subject_edit.txt',form=1,con='subject',action='insert',data=Subject())
        return page.render()

#    def render(self):
#		return dzpage.Page(dzutil.render('views/subject_show.txt',self)).render()


